package com.module.overview.ui.cardHistoryDetails.provider

import android.view.View.GONE
import android.view.View.VISIBLE
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.provider.BaseItemProvider
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.common.app.core.RouterHub
import com.common.app.data.bean.KeyBundle
import com.common.app.data.bean.overView.HistoryDetailsModel
import com.common.app.helper.RouterHelper
import com.common.base.app.extras.doOnIOThread
import com.common.base.app.extras.doOnUiThread
import com.common.base.utils.ViewUtils
import com.module.ble.repo.SportRecordRepo
import com.module.ble.utils.AllCardTypeConfig
import com.module.overview.R

/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：2025/7/14
 *
 * 描述：运动记录卡片
 *
 * 修订历史：完成度100%
 *
 */
class OvActivityRecordCardProvider(time:Long,
    override val itemViewType: Int = AllCardTypeConfig.TYPE_CARD_ACTIVITY_RECORD,
    override val layoutId: Int = R.layout.overview_activity_record_crad_provider
) : BaseItemProvider<HistoryDetailsModel>() {

    private var curTime = time

    private var tvNotRecordHint: TextView? = null

    private val  mAdapter by lazy { OvActivityRecordItemAdapter() }

    init {
    }

    override fun onViewHolderCreated(viewHolder: BaseViewHolder, viewType: Int) {
        tvNotRecordHint = viewHolder.getView(R.id.tvNotRecordHint)
        super.onViewHolderCreated(viewHolder, viewType)
    }


    override fun convert(helper: BaseViewHolder, item: HistoryDetailsModel) {
        helper.getView<RecyclerView>(R.id.hrvRecord).apply{
            adapter = mAdapter
        }

        tvNotRecordHint?.visibility = GONE

        getTheDaySportsData()

        mAdapter.setOnItemClickListener { _, _, position ->
            if (ViewUtils.isFastClick()) return@setOnItemClickListener //防止多次点击
            RouterHelper.navigationPostcard(RouterHub.ROUTER_OVERVIEW_EXERCISE_DETAILS)
                .withLong(KeyBundle.BUNDLE_KEY,  mAdapter.data[position].startTs)
                .navigation()
        }
    }

    /**
     * 获取当天运动信息
     */
    fun getTheDaySportsData(){
        doOnIOThread {
            SportRecordRepo.instance.findAllByDay(curTime)?.let { originalListIt->
                if (originalListIt.isNullOrEmpty()) return@let
//                val listItStr = JsonUtils.toJson(originalListIt)

                doOnUiThread {

                    mAdapter.setList(originalListIt)

                    tvNotRecordHint?.visibility = if (mAdapter.data.isNullOrEmpty()) VISIBLE else GONE
                }
            }
        }
    }
}